// pages/xuecar/orderdetail.js
var util = require('../../utils/util.js');
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        bannerUrl: '',
        orderid:0,
        orderinfo:[],
        isshow:true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
          orderid: options.oid
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      this.getOrderdetail();
        this.initBanner();
    },
    //广告图片初始化
    initBanner: function () {
        var that = this;
        var requrl = app.globalData.hostUrl + 'common/banner';
        var imgbanner = wx.getStorageSync('signupBanner');
        //console.log(imgbanner.length);
        if (!app.expiratTime('signupBanner') || imgbanner.length == 0) {
            wx.request({
                url: requrl,
                data: { 'location': '202' },
                header: {
                    'grant-appid': app.globalData.appid
                },
                success: function (res) {
                    if (res.statusCode === 200) {
                        wx.setStorageSync('signupBanner', res.data.result[0].imgpath);
                        that.setData({
                            bannerUrl: res.data.result[0].imgpath
                        })
                    }
                }
            })
        }
        else {
            that.setData({
                bannerUrl: imgbanner
            })
        }
    },
    getOrderdetail:function(){
      var that=this;
      var requrl = app.globalData.hostUrl + 'handler/orderdetail';
      wx.request({
        url: requrl,
        data: { 'oid': that.data.orderid },
        header: {
          'grant-appid': app.globalData.appid
        },
        success: function (res) {
          if (res.data.code == 200) {
            var orderinfo = res.data.result;
            var _isshow=true;
            if (orderinfo.status!=0){
              _isshow = false;
            }
            that.setData({
              orderinfo: orderinfo,
              isshow:_isshow
            })
          }
        }
      })
    },
    confirmpay:function(){
      var that=this;
      var clickflag=1;
      var postdata={
        'openid': wx.getStorageSync('openId'),
        'uid': wx.getStorageSync('userId'),
        'oid': this.data.orderid,
        'flag':'xc'
      };
      var requrl = app.globalData.hostUrl + 'handler/orderPrepay';
      if (clickflag==1){
        clickflag = 0;
        wx.request({
          url: requrl,
          data: postdata,
          header: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'grant-appid': app.globalData.appid
          },
          method: 'POST',
          success: function (res) {
              // console.log(res);
              if (res.data.code == 200) {
                // console.log('调起支付');
                wx.requestPayment({
                  'timeStamp': res.data.result.timeStamp,
                  'nonceStr': res.data.result.nonceStr,
                  'package': res.data.result.package,
                  'signType': 'MD5',
                  'paySign': res.data.result.paySign,
                  'success': function (res) {
                    wx.showModal({
                      title: '提示',
                      content: '支付成功!',
                      confirmText: "预约学车",
                      cancelText: "稍后再说",
                      success: function (res) {
                        if (res.confirm) {
                          wx.navigateTo({
                            url: '../myself/myappointment',
                          })
                        } else {
                          wx.navigateTo({
                            url: '../myself/myappointment',
                          })
                        }
                      }
                    });
                  },
                  'fail': function (res) {

                  },
                  'complete': function (res) {
                    wx.navigateTo({
                      url: '../myself/myappointment',
                    })
                  }
                });
              }
              else {
                util.isError(res.data.result, that);
              }
              clickflag = 1;
          }
        })
      }
    }
    
})